package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.view.View;
import android.widget.TextView;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.utils.ToastUtil;
import com.monkeygroup.mygoplus.ecchat.ui.contact.entity.ContactMemberDTO;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/21.
 */
public class AddFriendSearchResult extends BaseActivity implements View.OnClickListener {
    private TextView user_nickname;
    private ContactMemberDTO memberInfo = null;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_friend_search_result);
    }

    @Override
    public void initView() {
        user_nickname = (TextView) findViewById(R.id.user_nickname);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
        findViewById(R.id.btn_add_friend_request).setOnClickListener(this);
    }

    @Override
    public void doEvent() {
        memberInfo = (ContactMemberDTO) getIntent().getExtras().getSerializable("result");
        user_nickname.setText(memberInfo.getName());
    }

    /**
     * 發送添加好友請求
     **/
    private void addFriend() {
        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestAddFriend;
        HttpModel httpModel = new HttpModel(requestUrl, getParams(), new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    private Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);
        params.put("friendMobile", memberInfo.getId());

        return params;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.btn_add_friend_request:
                addFriend();
                break;
            default:
                break;
        }
    }
}